//FACEBOOK ASYNC CLIENT

//FacebookAsyncUser methods

#include "facebookasync.h"

#include "xmlParser.h"
#include "md5.h"
#include <fstream>
#include <iostream>

//FETCHING PHOTOS RELATED ROUTINES

bool FacebookAsyncUser::StartFetchingPhotos()
{
	assert(m_CurLoginStage == LOGINSTAGE_SUCCEED);
	assert(m_CurInfoStage == INFOSTAGE_SUCCEED);
	assert(m_CurPhotoStage == PHOTOSTAGE_NOTSTARTED || m_CurPhotoStage == PHOTOSTAGE_FAILED);

	m_CurPhotoStage = PHOTOSTAGE_NOTSTARTED;

	m_UserPhoto.clear();

	int i;

	m_FriendsPhotos.resize(m_FriendsIDs.size());

	for (i = 0; i < (int)m_FriendsPhotos.size(); i++)
		m_FriendsPhotos.at(i).clear(); 

	//starting first photo stage
	PhotoStartStage_GettingUserPhoto();

	return true;
}//bool FacebookAsyncUser::StartLogin()


void FacebookAsyncUser::PhotoStartStage_GettingUserPhoto()
{
	assert(m_HasActiveRequest == false);

	FacebookAsync::Instance().Helper_PrepareDownloadPhotoRequest(this, m_UserProfileInfo.PhotoURL);   

	PushNewRequest();

	m_CurPhotoStage = PHOTOSTAGE_GETTINGUSERPHOTO; 

	FacebookAsync::Instance().MakeCallback();

}


void FacebookAsyncUser::PhotoOnStageDone_GettingUserPhoto()
{
	assert(m_CurPhotoStage == PHOTOSTAGE_GETTINGUSERPHOTO);

	PullCompletedRequest();	

	m_UserPhoto = m_CurRequestResult;

    PhotoStartStage_GettingFriendsPhotos(); 
}


void FacebookAsyncUser::PhotoStartStage_GettingFriendsPhotos()
{

	assert(m_HasActiveRequest == false);

	m_CurFriendToProcess = 0;

	PhotoContinueStage_GettingFriendsPhotos();

}


void FacebookAsyncUser::PhotoContinueStage_GettingFriendsPhotos()
{

	assert(m_HasActiveRequest == false);

	while(m_CurFriendToProcess < (int)m_FriendsProfileInfo.size())
	{

		if (m_FriendsProfileInfo.at(m_CurFriendToProcess).PhotoURL.size() > 3)
			break;
		else
		{
			m_FriendsPhotos.at(m_CurFriendToProcess).clear();
			m_CurFriendToProcess++;
		}
	}

	if (m_CurFriendToProcess >= (int)m_FriendsProfileInfo.size())
	{
		//no photo sizes to fetch
		FacebookAsync::Instance().SetErrorDescription("");
		m_CurPhotoStage = PHOTOSTAGE_SUCCEED;
		FacebookAsync::Instance().MakeCallback();
		return;
	}

	FacebookAsync::Instance().Helper_PrepareDownloadPhotoRequest(this, m_FriendsProfileInfo.at(m_CurFriendToProcess).PhotoURL);   

	PushNewRequest();

	m_CurPhotoStage = PHOTOSTAGE_GETTINGFRIENDSPHOTOS; 

	FacebookAsync::Instance().MakeCallback();
}


void FacebookAsyncUser::PhotoOnStageDone_GettingFriendsPhotos()
{


	assert(m_CurPhotoStage == PHOTOSTAGE_GETTINGFRIENDSPHOTOS);

	PullCompletedRequest();

	m_FriendsPhotos.at(m_CurFriendToProcess) = m_CurRequestResult; 

	m_CurFriendToProcess++;

	PhotoContinueStage_GettingFriendsPhotos();

}
